<?php
if (!defined("MCTDESIGN")) die("# ");

$pid = isset($_GET["pid"]) ? intval($_GET["pid"]) : 0;
// xoa thu muc
if (isset($_POST['delete']) OR $_POST['flag'] == 1) {

	$total = isset($_POST["total"]) ? intval($_POST["total"]) : 0;

 	for($i=1;$i<=$total;$i++){

		$checkbox[$i] = isset($_POST["checkbox_$i"]) ? $_POST["checkbox_$i"] : '';

		if (!empty($checkbox[$i])) {

			$mysql->query("DELETE FROM ".$tb_fix."_category WHERE cat_id='".$checkbox[$i]."'");
            $mysql->query("DELETE FROM ".$tb_fix."_product 
                              WHERE pro_catrid='".$checkbox[$i]."'
                                    OR pro_catpid='".$checkbox[$i]."'
                                    OR pro_catid='".$checkbox[$i]."'       
                    ");
		}

	}

	$html_body = _html_trans("Đã xoá các danh mục thành công !");

	return false;

}
// sap xep thu muc
if (isset($_POST['order'])) {

	$total = isset($_POST["total"]) ? intval($_POST["total"]) : 0;

    for ($i=1;$i<=$total;$i++)

      {

           $cat_id    = isset($_POST["order_id_$i"])    ? $_POST["order_id_$i"]    : 0;

           $cat_order = isset($_POST["order_$i"]) ? $_POST["order_$i"] : 0;

           if ( ($cat_id>0)&&($cat_order>0) )

           {		

				$mysql->query("UPDATE ".$tb_fix."_category SET cat_order='$cat_order' WHERE cat_id='".$cat_id."'");

           }

    }

	$html_body = _html_trans("Đã cập nhật thứ tự các danh mục thành công !");

	return false;

}
// cap nhat thu muc
if (isset($_POST['update'])) {

	$cat_title = isset($_POST["cat_title"]) ? fixquotes(stripslashes($_POST["cat_title"])) : "";

	$cat_parent = isset($_POST["cat_parent"]) ? intval(stripslashes($_POST["cat_parent"])) : 0;

	$cat_description = isset($_POST["cat_description"]) ? fixquotes(stripslashes($_POST["cat_description"])) : "";
	$cat_img = isset($_FILES['cat_img']['name'] ) ? $_FILES['cat_img']['name'] : '';
	$cat_tmp_img = isset($_FILES['cat_img']['tmp_name'] ) ? $_FILES['cat_img']['tmp_name'] : '';

	if (empty($cat_title)) {

		$html_body = _html_trans("Tên của danh mục không được bỏ trống !");

		return false;

	}
	if (!empty($cat_img)) {
		$cat_img = uploadIMG($cat_img,$cat_tmp_img,"catimg");
		$SQL_IMG = ",cat_img='$cat_img'";
	}

    $mysql->query("UPDATE ".$tb_fix."_category SET cat_title='".$cat_title."', cat_description='$cat_description',cat_img='$cat_img',cat_parent='$cat_parent' WHERE cat_id='".$id."'");

	$html_body = _html_trans("Đã cập nhật thông tin danh mục thành công !","?obj=Category");

	return false;

}
// them thu muc
if (isset($_POST['add'])) {

	$cat_title = isset($_POST["cat_title"]) ? fixquotes(stripslashes($_POST["cat_title"])) : "";

	$cat_parent = isset($_POST["cat_parent"]) ? intval(stripslashes($_POST["cat_parent"])) : 0;

	$cat_description = isset($_POST["cat_description"]) ? fixquotes(stripslashes($_POST["cat_description"])) : "";
	$cat_img = isset($_FILES['cat_img']['name'] ) ? $_FILES['cat_img']['name'] : '';
	$cat_tmp_img = isset($_FILES['cat_img']['tmp_name'] ) ? $_FILES['cat_img']['tmp_name'] : '';

	if (empty($cat_title)) {

		$html_body = _html_trans("Bạn phải nhập tên của danh mục !");

		return false;

	}
	$cat_img = uploadIMG($cat_img,$cat_tmp_img,"catimg");

	$q = $mysql->query("SELECT * FROM ".$tb_fix."_category WHERE cat_parent='$cat_parent' ORDER BY cat_order DESC");

	$r = $mysql->num_rows($q);

	$order = $r+1;

	$mysql->query("INSERT INTO ".$tb_fix."_category (cat_title ,cat_description, cat_img ,cat_parent ,cat_order ) VALUES ('$cat_title','$cat_description','$cat_img','$cat_parent','$order')");

	$html_body = _html_trans("Đã thêm danh mục mới thành công !");

	return false;

}



function _list_select_category($_id="") {

	global $mysql, $tb_fix;

	$q = $mysql->query("SELECT * FROM ".$tb_fix."_category WHERE cat_parent='0' ORDER BY cat_parent ASC");

	$t = $mysql->num_rows($q);

	$_list = "<option name=\"cat_parent\" id=\"cat_parent\" value=\"0\" selected=\"selected\">Làm danh mục chính</option>";

	if ($t>0) {

		while ($r = $mysql->fetch_array($q)) {

			if ($_id == $r["cat_id"]) $_list .= "<option value=\"".$r["cat_id"]."\" selected=\"selected\">".$r["cat_title"]."</option>";

			else $_list .= "<option value=\"".$r["cat_id"]."\">".$r["cat_title"]."</option>";
			
			$q2 = $mysql->query("SELECT * FROM ".$tb_fix."_category WHERE cat_parent='".$r["cat_id"]."' ORDER BY cat_parent ASC");

			while ($r2 = $mysql->fetch_array($q2)) {
				if ($_id == $r2["cat_id"]) $_list .= "<option value=\"".$r2["cat_id"]."\" selected=\"selected\">|---".$r2["cat_title"]."</option>";

				else $_list .= "<option value=\"".$r2["cat_id"]."\">|---".$r2["cat_title"]."</option>";
			}


		}

	}

	return $_list;

}

$_c = 0;

function show_managerCategory($parent="0", $space=0){

	global $mysql, $tb_fix, $_c;

	$q = $mysql->query("SELECT * FROM ".$tb_fix."_category WHERE cat_parent='$parent' ORDER BY cat_order ASC");
    if($mysql->num_rows($q) > 0){
    $html = "";    
	while ($r = $mysql->fetch_array($q)) {

			$_c++;

			if ($r["cat_parent"]==0) {

				$_b = "<b>";

				$space = "";

			}

			else {

				$_b = "";

				$space = "&nbsp;&nbsp;&nbsp;&nbsp;";

			}
            
			$html .= "<tr class=\"row1\">";
            $html .= "<td align=\"center\" width=\"5%\" style=\"text-align:center;\"><input type='checkbox' id='checkbox' name='checkbox_".$_c."' value='".$r["cat_id"]."'></td>";
			$html .= "<td style='' align=\"center\" width=\"5%\"><input name='order_id_".$_c."' type='hidden' value='".$r["cat_id"]."'> <input name='order_".$_c."' type='text' size='2' maxlength='15' value='".$r["cat_order"]."'></td>";

			$html .= "<td width=\"75%\" align=\"left\" id=\"cat_".$r["cat_id"]."\" ><div style=\"margin-left:5px;\"><a href=\"?obj=Category&pid=".$r["cat_id"]."\"><b>".$r["cat_title"]."</b></a></span></div></td>";

			$html .= "<td width=\"13%\" id=\"adm_action\">
                    <div id='adm_edit' title='sửa nội dung' onclick=\"redirect('?obj=Category&act=edit&id=".$r["cat_id"]."')\"></div>
                    <div id='adm_del' title='xóa nội dung' onclick='check_del_row(".$r["cat_id"].")'></div>
            </td>";
			$html .= "</tr>";
            
			//$html .= show_managerCategory($parent="".$r["cat_id"]."", $space);

	}
    
    }else{
        $html .= "<tr class=\"row1\">";
        $html .= "<td colspan = '7' style='text-align: center;'>";
        $html .= "Không có danh mục con";
        $html .= "</td>";
        $html .= "</tr>";
    }
	return $html;

}


function _html_addCategory()

{

	global $dir_tpl, $mysql, $tb_fix,$temp;

	$temp->assign( 'base_tpl', "".$dir_tpl."" );

	$_html = $temp->fetch( '_adm_admin_add.mct' );

	return $_html;

}
function get_parent_names($cat_id, &$str) {
	global $mysql, $tb_fix, $stop;
	$query = $mysql->query("SELECT * FROM ".$tb_fix."_category WHERE cat_id='$cat_id' ORDER BY cat_order ASC");
	while ($result = $mysql->fetch_array($query)){
		if ( $cat_id == $result['cat_id'] ){
			get_parent_names($result['cat_parent'],  $str);
			$str .= ' <a href="?obj=Category&pid='. $result['cat_id'] .'">'. $result['cat_title'] .'</a> <span style="vertical-align: text-bottom;">»»</span> ';           
		}
	}
}
function _html_manageCategory()

{

	global $dir_tpl, $mysql, $tb_fix,$admin_id,$pid,$_c,$temp,$anyDB;

	$temp_body = $temp;

	$list_cat_parent = _list_select_category();

	$temp_body->assign( 'list_cat_parent', "".$list_cat_parent."" );
    // breakcum category
	if ($pid!=0) {
	   $child_cat = "";
		$q = $anyDB->query("SELECT * FROM ".$tb_fix."_category WHERE cat_id='$pid' ORDER BY cat_order ASC LIMIT 1");
		$r = $anyDB->getNext($q);
		get_parent_names($pid, $parent_name);
		$child_cat .= "<tr class=\"row1\">";
		$child_cat .= "<td colspan=\"4\" style=\"background:#b5d2e8\"><div style=\"padding:2px;text-align:left;\"> <b>$parent_name</b></div></td>";
		$child_cat .= "</tr>";
        $temp_body->assign( "child_cat", $child_cat );
	}
    // list category
    $sql = "SELECT * FROM ".$tb_fix."_category WHERE cat_parent='$pid' ORDER BY cat_order ASC";
    $q = $anyDB->query($sql);
    $r = $anyDB->getAll($q);
    $temp_body->assign("cat",$r);
    $temp_body->assign('dir_tpl',$dir_tpl);
    $temp_body->assign("breakcum",breakcum("Quản lý danh mục sản phẩm "));
	$_html = $temp_body->fetch( '_adm_category.mct' );
	return $_html;
}

function _html_editCategory($_id="")

{

	global $dir_tpl, $mysql, $tb_fix,$admin_id,$admin_permissions,$temp;

	$q = $mysql->query("SELECT * FROM ".$tb_fix."_category WHERE cat_id='$_id' ORDER BY cat_id ASC LIMIT 1");

	$t = $mysql->num_rows($q);

	if ($t>0) {

		$r = $mysql->fetch_array($q);

		$temp_body = $temp;

		$list_cat_parent = _list_select_category($r["cat_parent"]);

		$temp_body->assign( 'list_cat_parent', "".$list_cat_parent."" );

		$temp_body->assign(array(

								"cat_title" => "".$r["cat_title"]."",

								"cat_description" => "".$r["cat_description"]."",

								"cat_id" => "".$r["cat_id"].""

							)

						);
		if (!empty($r["cat_img"])) $temp_body->assign( 'cat_img', "<img width=\"120px\" src=\"".$r["cat_img"]."\">" );
        $temp_body->assign("breakcum",breakcum("Sửa danh mục sản phẩm "));
		$_html = $temp_body->fetch( '_adm_category_edit.mct' );

	}

	return $_html;

}

if ($act=="edit") {

	$html_body = _html_editCategory($id);

	return false;

}

$html_body = _html_manageCategory();



?>